﻿---@diagnostic disable: trailing-space
local extension = Package:new("moepack_moesp")
extension.extensionName = "moepack"


local zhangchangpu = General(extension, "moesp__zhangchangpu", "moe", 3, 3, General.Female)
zhangchangpu.subkingdom = "wei"
local emo__yanjiao = fk.CreateActiveSkill{
  name = "emo__yanjiao",
  anim_type = "support",
  mute = true,
  card_num = 0,
  target_num = 1,
  prompt = "#emo__yanjiao",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected)
    return #selected == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    player:broadcastSkillInvoke(self.name, math.random(2))
    room:notifySkillInvoked(player, self.name)
    local num = math.min(6, player:getMark("@emo__yanjiao"))
    local n = 4 + num
    room:removePlayerMark(player, "@emo__yanjiao", num)
    local cards = room:getNCards(n)
    room:moveCards({
      ids = cards,
      toArea = Card.Processing,
      moveReason = fk.ReasonPut,
      skillName = self.name,
      proposer = player.id,
    })
    local data_table = {}
    for _, p in ipairs(room.players) do
      data_table[p.id] = {
        cards,
        "Group1",
        "Group2",
        p == target
      }
    end
    room:askForMiniGame(room.players, self.name, "yanjiao", data_table)
    local cardmap = (target.client_reply)
    local rest, pile1, pile2 = cards, {}, {}
    if #cardmap == 3 then
      rest, pile1, pile2 = cardmap[1], cardmap[2], cardmap[3]
    end
    local moveInfos = {}
    if #pile1 > 0 and #pile2 > 0 then
      player:skip(Player.Discard)
      local chosen = room:askForCardChosen(player, player,  { card_data = { {"Group1", pile1}, {"Group2", pile2}  } }, self.name, "#emo__yanjiao-choice")
      local get = table.contains(pile1, chosen) and pile1 or pile2
      table.insert(moveInfos, {
        ids = get,
        to = player.id,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonPrey,
        skillName = self.name,
        moveVisible = true,
      })
      local toId
      if target == player then
        local tos = room:askForChoosePlayers(player, table.map(room:getOtherPlayers(player), Util.IdMapper), 1, 1, "#emo__yanjiao-choose", self.name, true)
        if #tos > 0 then
          toId = tos[1]
        end
      else
        toId = target.id
      end
      local other = table.contains(pile2, chosen) and pile1 or pile2
      if toId then
        table.insert(moveInfos, {
          ids = other,
          to = toId,
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonGive,
          skillName = self.name,
          moveVisible = true,
          proposer = player.id,
        })
      else
        table.insertTable(rest, other)
      end
    else
      -- 失败
      player:broadcastSkillInvoke(self.name, 3)
    end
    if #rest > 0 then
      table.insert(moveInfos, {
        ids = rest,
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonPutIntoDiscardPile,
        proposer = player.id,
        skillName = self.name,
      })
    end
    room:moveCards(table.unpack(moveInfos))
  end,
}
zhangchangpu:addSkill(emo__yanjiao)

local emo__xingshen = fk.CreateTriggerSkill{
  name = "emo__xingshen",
  anim_type = "masochism",
  events = {fk.Damaged},
  on_trigger = function(self, event, target, player, data)
    self.cancel_cost = false
    for i = 1, data.damage do
      self:doCost(event, target, player, data)
      if self.cancel_cost or not player:hasSkill(self) then break end
    end
  end,
  on_cost = function (self, event, target, player, data)
    if player.room:askForSkillInvoke(player, self.name) then
      return true
    end
    self.cancel_cost = true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(2, self.name)
    if player.dead then return end
    room:addPlayerMark(player, "@emo__yanjiao", 2)
  end,
}
zhangchangpu:addSkill(emo__xingshen)

Fk:loadTranslationTable{
  ["moesp__zhangchangpu"] = "张昌蒲",
  ["designer:moesp__zhangchangpu"] = "北冰洋",
  ["#moesp__zhangchangpu"] = "矜严明训",

  ["emo__yanjiao"] = "严教",
  [":emo__yanjiao"] = "出牌阶段限一次，你可以选择一名角色并从牌堆顶亮出四张牌，该角色将这些牌分成点数之和相等的两组，你选择获得其中一组，若选择的角色不为你，该角色获得另一组牌，否则你须将另一组牌交给一名其他角色或置入弃牌堆。最后将剩余未分组的牌置于弃牌堆。若成功分组，你跳过本回合弃牌阶段。",
  ["Group1"] = "第一组",
  ["Group2"] = "第二组",
  ["#emo__yanjiao"]= "严教：对一名角色发动“严教”",
  ["@emo__yanjiao"] = "严教",
  ["#emo__yanjiao-choice"] = "严教：选择你获得的一组",
  ["#emo__yanjiao-choose"] = "严教：将另一组交给一名其他角色。点“取消”：弃置",

  ["emo__xingshen"] = "省身",
  [":emo__xingshen"] = "每当你受到1点伤害后，你可以摸两张牌且获得2个“省身”标记。你下一次发动〖严教〗时移除所有（至多6个）“省身”标记，增加等量张亮出的牌。",

  ["$emo__yanjiao1"] = "书卷精妙，需早学勤记。",
  ["$emo__yanjiao2"] = "幼时不学，弱冠智愚。",
  ["$emo__yanjiao3"] = "幼儿顽劣，需严加管教。",
  ["$emo__xingshen1"] = "清新省身，淡然虚静。",
  ["$emo__xingshen2"] = "三省吾身，方能知清寻源。",
  ["~moesp__zhangchangpu"] = "家事不宁，国事不安。",
}







local emo__woshou = fk.CreateActiveSkill{
  name = "emo__woshou",
  mute = true,
  switch_skill_name = "emo__woshou",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) < (1 + player:getMark("emo__woshou_updata"))
  end,
  prompt = function (self)
    return "#emo__woshou-prompt"..Self:getSwitchSkillState(self.name)
  end,
  target_num = 1,
  card_num = 1,
  card_filter = function (self, to_select, selected)
    return #selected == 0
  end,
  target_filter = function(self, to_select, selected, selected_cards)
    return #selected == 0 and Self.id ~= to_select and #selected_cards == 1
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local to = room:getPlayerById(effect.tos[1])
    local card = Fk:getCardById(effect.cards[1])
    local type, color = "emo__woshou_"..card:getTypeString(), card.color
    room:obtainCard(to, card, true, fk.ReasonGive, player.id)
    if to.dead then return end
    if player:getSwitchSkillState(self.name, true) == fk.SwitchYang then
      room:notifySkillInvoked(player, self.name, "support")
      player:broadcastSkillInvoke(self.name, math.random(2))
      if color == Card.Black then
        to:drawCards(2, self.name)
      elseif color == Card.Red and to:isWounded() then
        room:recover { num = 1, skillName = self.name, who = to, recoverBy = player }
      end
    else
      room:notifySkillInvoked(player, self.name, "control")
      player:broadcastSkillInvoke(self.name, 3)
      local mark = to:getTableMark("@emo__woshou")
      table.insertIfNeed(mark, type)
      room:setPlayerMark(to, "@emo__woshou", mark)
    end
  end
}
local emo__woshou_delay = fk.CreateTriggerSkill{
  name = "#emo__woshou_delay",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    return target == player and table.contains(player:getTableMark("@emo__woshou"), "emo__woshou_equip") 
  end,
  on_use = function(self, event, target, player, data)
    data.damage = data.damage - 1
  end,

  refresh_events = {fk.AfterTurnEnd},
  can_refresh = function (self, event, target, player, data)
    return target == player and player:getMark("@emo__woshou") ~= 0
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "@emo__woshou", 0)
  end,
}
emo__woshou:addRelatedSkill(emo__woshou_delay)

local emo__woshou_prohibit = fk.CreateProhibitSkill{
  name = "#emo__woshou_prohibit",
  is_prohibited = function(self, player, to, card)
    return table.contains(player:getTableMark("@emo__woshou"), "emo__woshou_basic")
    and player ~= to and card and card.color == Card.Black
  end,
}
emo__woshou:addRelatedSkill(emo__woshou_prohibit)

local emo__woshou_invalidity = fk.CreateInvaliditySkill {
  name = "#emo__woshou_invalidity",
  invalidity_func = function(self, player, skill)
    return table.contains(player:getTableMark("@emo__woshou"), "emo__woshou_trick")
    and not skill.name:endsWith("&") and not skill:isEquipmentSkill(player)
  end
}
emo__woshou:addRelatedSkill(emo__woshou_invalidity)


Fk:loadTranslationTable{
  ["emo__woshou"] = "握手",
  [":emo__woshou"] = "转换技，出牌阶段限一次，你可以交给一名其他角色一张牌，阳：若此牌为红色，其回复一点体力，若为黑色，其摸两张牌；阴：根据此牌的类型，其获得效果直到其下回合结束：基本牌，无法对其他角色使用黑色牌；装备牌：造成伤害-1；锦囊牌：技能失效。",

  ["#emo__woshou_delay"] = "握手",
  ["#emo__woshou-prompt0"] = "交给其他角色一张牌，红色:其回复一点体力；黑色:其摸两张牌",
  ["#emo__woshou-prompt1"] = "交给其他角色一张牌，基本:无法用黑色牌；装备:造成伤害-1；锦囊:技能失效",
  ["@emo__woshou"] = "握手:",
  ["emo__woshou_basic"] = "禁黑",
  ["emo__woshou_equip"] = "减伤",
  ["emo__woshou_trick"] = "封技",
}




local moesp__gilgamesh = General(extension, "moesp__gilgamesh", "god", 4, 4, General.Female)

local emo__tiansuo = fk.CreateActiveSkill{
  name = "emo__tiansuo",
  anim_type = "offensive",
  card_num = 0,
  min_target_num = 1,
  card_filter = Util.FalseFunc,
  prompt = "#emo__tiansuo-prompt",
  target_filter = function(self, to_select, selected)
    return to_select ~= Self.id and not table.contains(Self:getTableMark("emo__tiansuo_target"), to_select)
  end,
  can_use = function(self, player)
    return table.find(Fk:currentRoom().alive_players, function (p)
      return p ~= player and not table.contains(player:getTableMark("emo__tiansuo_target"), p.id)
    end)
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    room:sortPlayersByAction(effect.tos)
    local mark = player:getTableMark("emo__tiansuo_target")
    table.insertTable(mark, effect.tos)
    room:setPlayerMark(player, "emo__tiansuo_target", mark)
    for _, pid in ipairs(effect.tos) do
      local to = room:getPlayerById(pid)
      if not to.dead and not to.chained then
        to:setChainState(true)
      end
    end
    for _, pid in ipairs(effect.tos) do
      local to = room:getPlayerById(pid)
      if not to.dead then
        Fk.skills["emo__jinlv"]:use(nil, player, player, {to = to.id})
      end
    end
  end,
}
moesp__gilgamesh:addSkill(emo__tiansuo)

local emo__jinlv = fk.CreateTriggerSkill{
  name = "emo__jinlv",
  events = {fk.TargetSpecified},
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and data.firstTarget and data.to ~= player.id
     and not table.contains(player:getTableMark("emo__jinlv_target-phase"), data.to) then
      local to = player.room:getPlayerById(data.to)
      return not to.dead and not to:isNude() and U.isOnlyTarget(to, data, event)
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#emo__jinlv-invoke:"..data.to)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(data.to)
    local mark = player:getTableMark("emo__jinlv_target-phase")
    table.insertIfNeed(mark, to.id)
    room:setPlayerMark(player, "emo__jinlv_target-phase", mark)
    if player.dead or to.dead then return end
    local cards = to:getCardIds("he")
    if #cards > 0 then
      local cardId = cards[1]
      if #cards > 1 then
        cardId = room:askForCard(to, 1, 1, true, self.name, false, ".", "#emo__jinlv-give:"..player.id)[1]
      end
      room:moveCardTo(cardId, Player.Hand, player, fk.ReasonGive, self.name, nil, true, to.id)
      if player.dead then return end
      local c = Fk:getCardById(cardId)
      if table.contains(player:getCardIds("h"), cardId) and U.getDefaultTargets(player, c, true, false) then
        local type = c.type
        U.askForUseRealCard(room, player, {cardId}, ".", self.name, "#emo__jinlv-use:::"..c:toLogString(), nil, false, false)
        if player.dead then return end
        if type == Card.TypeEquip then
          if not to:isNude() then
            local cid = room:askForCardChosen(player, to, "he", self.name, "#emo__jinlv-throw:"..to.id)
            room:throwCard({cid}, self.name, to, player)
          end
          if not player:isNude() and not to.dead then
            local give = room:askForCard(player, 1, 1, true, self.name, false, ".", "#emo__jinlv-back:"..to.id)
            room:moveCardTo(give, Player.Hand, to, fk.ReasonGive, self.name, nil, true, player.id)
          end
        elseif type == Card.TypeBasic then
          if not to.dead then
            local use = room:askForUseCard(to, "slash", "slash", "#emo__jinlv-slash:"..player.id, true,
            {bypass_distances = true,bypass_times = true, exclusive_targets = {player.id}})
            if use then
              room:useCard(use)
            else
              room:loseHp(to, 1, self.name)
            end
          end
        else
          MoeFunc.allDraw({player,to}, 1, self.name)
          if not to:isNude() then
            room:askForDiscard(to, 1, 1, true, self.name, false)
          end
        end
        return
      end
    end
    room:useVirtualCard("slash", nil, player, to, self.name, true)
  end,
}
moesp__gilgamesh:addSkill(emo__jinlv)

local emo__guaili = fk.CreateActiveSkill{
  name = "emo__guaili",
  anim_type = "offensive",
  frequency = Skill.Limited,
  card_num = 0,
  target_num = 0,
  card_filter = Util.FalseFunc,
  prompt = "#emo__guaili-prompt",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    room:setPlayerMark(player, "@@emo__guaili-turn", 1)
    local n = 0
    for _, p in ipairs(room:getOtherPlayers(player)) do
      local slots = table.filter({Player.ArmorSlot, Player.DefensiveRideSlot, Player.TreasureSlot}, function (s)
        return table.contains(p:getAvailableEquipSlots(), s)
      end)
      for _, s in ipairs(slots) do
        if #p:getEquipments(Util.convertSubtypeAndEquipSlot(s)) > 0 then
          n = n + 1
        end
      end
      room:abortPlayerArea(p, slots)
    end
    if not player.dead then
      player:drawCards(n, self.name)
    end
  end,
}

local emo__guaili_delay = fk.CreateTriggerSkill{
  name = "#emo__guaili_delay",
  events = {fk.TurnEnd},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:getMark("@@emo__guaili-turn") > 0 then
      return #player.room.logic:getEventsOfScope(GameEvent.Death, 1, function(e)
        local death = e.data[1]
        return death.damage and death.damage.from == target
      end, Player.HistoryTurn) == 0
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:abortPlayerArea(player, player:getAvailableEquipSlots())
    if player.hp > 0 then
      room:loseHp(player, player.hp, "emo__guaili")
    end
  end,
}
emo__guaili:addRelatedSkill(emo__guaili_delay)
moesp__gilgamesh:addSkill(emo__guaili)


Fk:loadTranslationTable{
  ["moesp__gilgamesh"] = "吉尔伽美什",
  ["#moesp__gilgamesh"] = "金闪闪",
  ["designer:moesp__gilgamesh"] = "祈",
  
  ["emo__tiansuo"] = "天锁",
  [":emo__tiansuo"] = "出牌阶段，你可以选择任意名未选择过的其他角色，横置这些角色，然后依次对这些角色发动一次〖金律〗。",
  ["#emo__tiansuo-prompt"] = "天锁:横置任意名角色(每角色限一次)，然后对这些角色发动〖金律〗",

  ["emo__jinlv"] = "金律",
  [":emo__jinlv"] = "每阶段对每名角色限一次，当你使用牌指定其他角色为唯一目标后，若该角色有牌，你可以令其交给你一张牌，若你无法使用此牌，你视为对其使用一张【杀】，否则你须使用此牌，然后若此牌的类型为：基本牌：其需对你使用一张【杀】，否则失去一点体力；装备牌：你弃置其一张牌，然后交给其一张牌；锦囊牌：你与其各摸一张牌，然后其弃置一张牌。",
  ["#emo__jinlv-invoke"] = "金律:你可以令 %src 交给你一张牌，并使用之(无法使用则视为对其使用杀)",
  ["#emo__jinlv-give"] = "金律:交给 %src 一张牌，若其不能使用此牌，其视为对你使用【杀】",
  ["#emo__jinlv-use"] = "金律：你须使用 %arg ！",
  ["#emo__jinlv-back"] = "金律：交给 %src 一张牌",
  ["#emo__jinlv-throw"] = "金律：弃置 %src 一张牌",
  ["#emo__jinlv-slash"] = "金律：你需对 %src 使用【杀】，否则失去一点体力",

  ["emo__guaili"] = "乖离",
  [":emo__guaili"] = "限定技，出牌阶段，你可以废除所有其他角色的所有防具、防御坐骑和宝具栏，然后摸X张牌（X为此装备栏内的牌数），本回合结束时，若你没有杀死角色，你废除所有装备栏并失去所有体力。",
  ["#emo__guaili-prompt"] = "乖离：废除所有其他角色的所有防具、防御坐骑和宝具栏，然后摸X张牌（X为此装备栏内的牌数）",
  ["@@emo__guaili-turn"] = "乖离发动",
  ["#emo__guaili_delay"] = "乖离",
}



